package config

import (
	"gitee.com/gousing/values"
)

// GetInt
func GetInt(key string) int {
	return GetIntD(key, 0)
}
func GetIntD(key string, defaultVal int) int {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToIntE(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetIntE(key string) (int, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToIntE(v)
	}
}

// GetInt8
func GetInt8(key string) int8 {
	return GetInt8D(key, 0)
}
func GetInt8D(key string, defaultVal int8) int8 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToInt8E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetInt8E(key string) (int8, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToInt8E(v)
	}
}

// GetInt16
func GetInt16(key string) int16 {
	return GetInt16D(key, 0)
}
func GetInt16D(key string, defaultVal int16) int16 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToInt16E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetInt16E(key string) (int16, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToInt16E(v)
	}
}

// GetInt32
func GetInt32(key string) int32 {
	return GetInt32D(key, 0)
}
func GetInt32D(key string, defaultVal int32) int32 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToInt32E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetInt32E(key string) (int32, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToInt32E(v)
	}
}

// GetInt64
func GetInt64(key string) int64 {
	return GetInt64D(key, 0)
}
func GetInt64D(key string, defaultVal int64) int64 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToInt64E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetInt64E(key string) (int64, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToInt64E(v)
	}
}

// GetUint
func GetUint(key string) uint {
	return GetUintD(key, 0)
}
func GetUintD(key string, defaultVal uint) uint {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToUintE(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetUintE(key string) (uint, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToUintE(v)
	}
}

// GetUint8
func GetUint8(key string) uint8 {
	return GetUint8D(key, 0)
}
func GetUint8D(key string, defaultVal uint8) uint8 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToUint8E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetUint8E(key string) (uint8, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToUint8E(v)
	}
}

// GetUint16
func GetUint16(key string) uint16 {
	return GetUint16D(key, 0)
}
func GetUint16D(key string, defaultVal uint16) uint16 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToUint16E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetUint16E(key string) (uint16, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToUint16E(v)
	}
}

// GetUint32
func GetUint32(key string) uint32 {
	return GetUint32D(key, 0)
}
func GetUint32D(key string, defaultVal uint32) uint32 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToUint32E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetUint32E(key string) (uint32, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToUint32E(v)
	}
}

// GeUint64
func GetUint64(key string) uint64 {
	return GetUint64D(key, 0)
}
func GetUint64D(key string, defaultVal uint64) uint64 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToUint64E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetUint64E(key string) (uint64, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToUint64E(v)
	}
}

// GetFloat32
func GetFloat32(key string) float32 {
	return GetFloat32D(key, 0)
}
func GetFloat32D(key string, defaultVal float32) float32 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToFloat32E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetFloat32E(key string) (float32, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToFloat32E(v)
	}
}

// GetFloat64
func GetFloat64(key string) float64 {
	return GetFloat64D(key, 0)
}
func GetFloat64D(key string, defaultVal float64) float64 {
	if v, ok := GetExist(key); !ok {
		return defaultVal
	} else {
		if to, err := values.ToFloat64E(v); err != nil {
			return defaultVal
		} else {
			return to
		}
	}
}
func GetFloat64E(key string) (float64, error) {
	if v, ok := GetExist(key); !ok {
		return 0, ErrKeyNotExist
	} else {
		return values.ToFloat64E(v)
	}
}
